/* Assignment Notebook
 * Created By: Amanda Rush
 * www.AmandaRush.com
 * Version 1.1
 * Release: 4/14/2011
 */

package com.captainkat.asn.view;


import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Date;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;
import java.text.SimpleDateFormat;

import com.captainkat.asn.bo.CalendarHolder;
import com.captainkat.asn.bo.EventHolder;
import com.captainkat.asn.calendar.CalendarManager;
import com.captainkat.asn.R;

import android.graphics.Typeface;


//Date Picker
import android.content.DialogInterface;
import android.app.AlertDialog;


public class DailyView extends Activity implements OnClickListener {
	private TextView date;
	private TextView add;
	private TextView previous;
	private TextView next;
	private Calendar currentDate;
	private TimeZone timezone;
	private SimpleDateFormat dateFormat;
	private SimpleDateFormat timeFormat;
	private LinearLayout mainLinearLayout;
	private Typeface font;
	
	private CalendarManager calendarManager;
	
	protected String calendarId = "-1";
	protected String defaultCalendarId = "-1";

	
	//Date Picker
	static final int DATE_DIALOG_ID = 0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setRequestedOrientation(1);  
		setContentView(R.layout.main);
		
		font = Typeface.createFromAsset(getAssets(), "dandelion_in_the_spring.ttf");  
		
		
		calendarManager = new CalendarManager();
				
		//set up Views and Click Listeners
		date = (TextView) findViewById(R.id.date);
		date.setOnClickListener(this);
		date.setTypeface(font);
		
		add = (TextView) findViewById(R.id.add);
		add.setOnClickListener(this);
		add.setTypeface(font);
		
		//set up title objects???
		mainLinearLayout = (LinearLayout) findViewById(R.id.mainLinearLayout);
		previous = (TextView) findViewById(R.id.btnPrevious);
		previous.setOnClickListener(this);
		previous.setTypeface(font);
		
		next = (TextView) findViewById(R.id.btnNext);
		next.setOnClickListener(this);
		next.setTypeface(font);
		
		
		//get today's date on the Calendar
		currentDate = Calendar.getInstance();
		timezone = TimeZone.getDefault();
		currentDate.setTimeZone(timezone);
		dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		timeFormat = new SimpleDateFormat("hh:mm a");
		
		// look for a passed-in date.
		Bundle b;
		if (this.getIntent().getExtras() != null) {
		b = this.getIntent().getExtras();
		currentDate.setTimeInMillis(b.getLong("time"));
		}
		
		
		calendarManager.init(this);
		drawScreen();
		}
	
	public void drawScreen() {
		clearScreen();
		//title = new ArrayList<TextView>();
		
		calendarManager.createEventsListforDate(currentDate);
		EventHolder e; 
		TextView t = new TextView(mainLinearLayout.getContext());
		if (calendarManager.getEventHolder() !=null ) {
	    	for (Iterator<EventHolder> i = calendarManager.getEventHolder().iterator(); i.hasNext(); ) {
	    		e = new EventHolder(); 
	    		e= i.next(); 
				t = new TextView(mainLinearLayout.getContext());

	    		if (e.getAllDayEvent()==1) {
	    			
	    			
	    			t.setText(e.getTitle()); //edit the title of this textView;
					
	    		} else {

					t.setText(timeFormat.format(new Date(e.getDtstart()))
							+" "+e.getTitle()); //edit the title of this textView;
					
	    		}
	    		t.setId(Integer.parseInt(e.getEid())); //set the id so we can click on it and get to the right one!
				t.setOnClickListener(this); //set up a click listener
				//title.add(t);
				t.setTypeface(font);
				mainLinearLayout.addView(t); //add it to the view	
	    	}
	    	mainLinearLayout.addView(new TextView(this));
	    	mainLinearLayout.addView(new TextView(this));
		}
				
		
	} //end DrawScreen()
	@Override
	public void onClick(View v) {
		
		
		
	switch (v.getId()) { //what to do when you press one of the buttons
		
		case R.id.date:
			//bring the datepicker
			showDialog(DATE_DIALOG_ID);
			
			break;
		case R.id.btnPrevious:
			//change date && reload
			currentDate.add(Calendar.DAY_OF_MONTH, -1);
			//WE NEED TO RELOAD DATA
			drawScreen();
			//edit the intent
			changeIntentTime();
			break;
		case R.id.btnNext:
			//change date && reload
			currentDate.add(Calendar.DAY_OF_MONTH, 1);			
			//WE NEED TO RELOAD DATA
			drawScreen();
			//edit the intent
			changeIntentTime();
			break;
		case R.id.add:
			
			calendarManager.createCalendarList();
			List<String> items = new ArrayList<String>();
			CalendarHolder c = new CalendarHolder();
			for (Iterator<CalendarHolder> i = calendarManager.getCalendarHolder().iterator(); i.hasNext(); ) {
				c = i.next();
				items.add(c.getDisplayName());
	    	}
			
			String[] itemsArray = new String[items.size()];

			AlertDialog.Builder ab=new AlertDialog.Builder(DailyView.this);
			ab.setTitle("Choose a Calendar");
			
			
			ab.setSingleChoiceItems(items.toArray(itemsArray), -1,  new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface d, int choice) {
				//set the calendar Id equal to the calendar we pick
				calendarId = calendarManager.getCalendarHolder().get(choice).getCalendarId();
			
			}
			});
			ab.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface d, int choice) {
					// on OK button action
					//go on the the add screen
					Intent addAsn = new Intent(DailyView.this, AssignmentEdit.class);
		            Bundle b = new Bundle();
					b.putLong("time",currentDate.getTimeInMillis());
					b.putString("calendarId", calendarId);
					b.putBoolean("editing", false);
					addAsn.putExtras(b);
					startActivity(addAsn);
					finish();
					
					}
					});
			
			ab.show();			
   			break;
   			default: //if it is anything else, it must be an assignment title
   				openEvent(v.getId(),v);
   				break;
   				
			
		}



	}
	
	public void openEvent(int eid, View v) {
		Intent viewAsn = new Intent(v.getContext(), AssignmentView.class);
		Bundle b = new Bundle();
		b.putLong("time",currentDate.getTimeInMillis());
		//b.putString("calendarId", eventHolder.getCalendarId());
		b.putInt("eid",eid);
		viewAsn.putExtras(b);
        startActivity(viewAsn);
        finish();
		}
	
	
	public void clearScreen() {
		//clear the Assignments from the view
		mainLinearLayout.removeAllViews();
		//clear the titles in the list
		//title.removeAll(title);
		date.setText(dateFormat.format(currentDate.getTime()));
		
		}

	
	//DATE PICKER THINGS
	// Creating dialog


	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
	// onDateSet method
	public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
		//String date_selected = String.valueOf(monthOfYear)+"/"+String.valueOf(dayOfMonth)+"/"+String.valueOf(year);
		
		currentDate.set(Calendar.MONTH, monthOfYear);
		currentDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
		currentDate.set(Calendar.YEAR, year);
		
	//show the date on the screen
		date.setText(dateFormat.format(currentDate.getTime()));
		
		changeIntentTime();
		//WE NEED TO RELOAD DATA
		drawScreen();
		
	}
	};
	
	@Override
	protected Dialog onCreateDialog(int id) {
	
	int cyear = currentDate.get(Calendar.YEAR);
	int cmonth = currentDate.get(Calendar.MONTH);
	int cday = currentDate.get(Calendar.DAY_OF_MONTH);
	switch (id) {
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, mDateSetListener,  cyear, cmonth, cday);
				}
	return null;
	}
	
	public void changeIntentTime() {
		//edit the intent
		Bundle b = new Bundle();
		b.putLong("time", currentDate.getTimeInMillis());
		this.getIntent().replaceExtras(b);
	}
}
